<template>
  <div class='home-box'>
    <!-- 顶部导航 -->
    <van-nav-bar class='page-nav-bar' fixed>
      <van-button slot='title' icon="search" type="primary" class='btn-search' to='/search'>搜索</van-button>
    </van-nav-bar>
    <!-- 标签页 -->
    <van-tabs v-model="active" animated swipeable class='channel-tabs'>
      <van-tab v-for='item in channels' :key='item.id' :title="item.name">
        <Article  :channelId='item.id'></Article>
      </van-tab>
      <div slot='nav-right' class='nav-right' @click='isShowEditChannelPopup= true'>
        <i class='toutiao toutiao-gengduo'></i>
      </div>
      <div slot='nav-right' class='placeholder'></div>
    </van-tabs>
    <!-- 弹出层 -->
    <van-popup v-model='isShowEditChannelPopup' closeable position="bottom" close-icon-position='top-left'
      :style="{ height: '100%' }">
      <ChannelEdit @sendData='getEditChannelData' :active='active' :channels='channels'></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
// 编辑频道组件
import ChannelEdit from './components/channel-edit.vue'
// 文章列表组件
import Article from './components/article.vue'
// 用户频道的请求
import { getUserChannels } from '@/api/user.js'
// 本地获取数据
import { getItem } from '@/utils/storage.js'
export default {
  data () {
    return {
      // 标签页选中
      active: 0,
      // 用户频道的数据
      channels: [],
      // 弹出层显示
      isShowEditChannelPopup: false
    }
  },
  methods: {
    // 通过我的频道编辑进行关闭
    getEditChannelData (e) {
      this.isShowEditChannelPopup = e.close
      this.active = e.index
    },
    // 获取用户频道列表
    async loadUserChannels () {
      const data = getItem('hmtt_channel')
      if (!this.$store.state.user && getItem('hmtt_channel')) {
        this.channels = data
      } else {
        try {
          const res = await getUserChannels()
          // console.log(res)
          this.channels = res.data.data.channels
        } catch (err) {
          this.$toast('获取失败')
        }
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    // 文章列表组件
    Article,
    // 编辑频道组件
    ChannelEdit
  },
  created () {
    // 获取用户频道列表
    this.loadUserChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home-box {
  padding-bottom: 100px;
  padding-top: 174px;
  .btn-search {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 32px;
    border: none;
  }
  /deep/.van-icon-search {
    color: #fff;
  }
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  /deep/.channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .van-tab {
      height: 82px;
      min-width: 200px;
      border-right: 1px solid #edeff3;
      .van-tab__text {
        font-size: 28px;
        color: #777;
      }
    }
    .van-tab--active {
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3296fa;
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .nav-right {
      width: 66px;
      height: 82px;
      position: fixed;
      right: 0;
      background: rgba(255, 255, 255, 0.9);
      display: flex;
      justify-content: center;
      align-items: center;
      .toutiao {
        font-size: 33px;
      }
      &::before {
        content: '';
        width: 2px;
        height: 82px;
        background-image: url(~@/assets/gradient-gray-line.png);
        position: absolute;
        left: 0;
        background-size: 100% 100%;
      }
    }
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }
}
</style>
